package pattern.singleton.demo8;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Constructor;

/**
 * @Author suzhengzhuang
 * @Email: suzhengzhuang@icloud.com
 * @Date 2024/4/17 10:08
 * @PackageName:pattern.singleton.demo1
 * @ClassName: Client
 * @Description: TODO 通过反射破坏单例模式
 * @Version 1.0
 */
public class Client {

    public static void main(String[] args) throws Exception {

        // 1.获取Singleton的字节码对象
        Class singletonClass = Singleton.class;
        // 2.获取无参构造方法
        Constructor declaredConstructor = singletonClass.getDeclaredConstructor();
        // 3.取消访问检查
        declaredConstructor.setAccessible(true);
        // 4.通过反射的方式创建Singleton对象
        Singleton singleton = (Singleton) declaredConstructor.newInstance();
        Singleton singleton1 = (Singleton) declaredConstructor.newInstance();
        // 如果返回的ture说明并没有破坏单例模式，如果是false说明破坏了单例模式
        System.out.println(singleton == singleton1);
    }

}
